﻿
package com.android.finaltracer.usermanage;

import android.os.Handler;
import android.os.Message;

public class FtTimer extends Handler {
    private static FtTimer instance;

    public static final long DEFAULT_DELAY = 5 * 60 * 1000;

    public static final long DEFAULT_SHORT_DELAY = DEFAULT_DELAY;

    public static final long DEFAULT_LONG_DELAY = (long) (DEFAULT_DELAY * 2.5);

    public static final int TIMER_ID_BIND_WAIT_BIND_CNF = 0;
    public static final int TIMER_ID_BIND_WAIT_BIND_RSP = TIMER_ID_BIND_WAIT_BIND_CNF + 1;

    // SIM卡丢失的那个手机 发送信息之后 并没有状态等待需要一个timer
    public static final int TIMER_ID_COMMAND_WAIT_HANDSHANKE_CNF = TIMER_ID_BIND_WAIT_BIND_RSP + 1;
    public static final int TIMER_ID_COMMAND_WAIT_MASTER_ORDER = TIMER_ID_COMMAND_WAIT_HANDSHANKE_CNF + 1;
    public static final int TIMER_ID_COMMAND_WAIT_GPS_INFO = TIMER_ID_COMMAND_WAIT_MASTER_ORDER + 1;
    // 同步timerid
    public static final int TIMER_ID_COMMAND_WAIT_PERIOD_CHECK = TIMER_ID_COMMAND_WAIT_GPS_INFO + 1;
    public static final int TIMER_ID_COMMAND_WAIT_SYNC_CNF = TIMER_ID_COMMAND_WAIT_PERIOD_CHECK + 1;

    private FtTimer(Handler.Callback callback) {
        super(callback);
    }

    public static void initInstance(Handler.Callback callback) {
        if (instance == null) {
            instance = new FtTimer(callback);
        }
    }

    public static void startTimer(int module, int timerId, String phoneNum,
            long delayMillis) {
        if (instance == null) {
            return;
        }
        instance.start(module, timerId, phoneNum, delayMillis);
    }

    public static void startTimer(int module, int timerId, String phoneNum) {
        if (instance == null) {
            return;
        }
        instance.start(module, timerId, phoneNum);
    }

    public static void stopTimer(int module, String phone) {
        if (instance == null) {
            return;
        }
        instance.stop(module, phone);
    }

    public static void stopAllTimer() {
        if (instance == null) {
            return;
        }
        instance.stopAll();
    }

    private void start(int module, int timerId, String phoneNum,
            long delayMillis) {
        this.sendMessageDelayed(
                Message.obtain(null, module, timerId, 0, phoneNum), delayMillis);
    }

    private void start(int module, int timerId, String phoneNum) {
        this.sendMessageDelayed(
                Message.obtain(null, module, timerId, 0, phoneNum),
                DEFAULT_DELAY);
    }

    private void stop(int module, String phone) {
        this.removeMessages(module, phone);
    }

    private void stopAll() {
        this.removeMessages(0, null);
    }
}
